package com.xoa.service.attend.wrapper;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.attend.Attend;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;


public class Test {

    public static Log log = LogFactory.getLog(Test.class);

    public static <list> void main(String[] args) throws IOException {
        String  url = "http://127.0.0.1:88/api/v2/transaction/get/?key=34caai0tm1ajpduoy5gxoaihapdebynbslg7rnqo38np";
        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String starttime = format.format(new Date()) + " 00:00:00";
        //每日结束时间时间  endtime
        String endtime = format.format(new Date()) + " 23:59:59";

        map.put("starttime", starttime);
        map.put("endtime", endtime);
        String param = JSONObject.toJSONString(map);
        String result = sendPost(url,param);

        /*JSONArray json = new JSONArray();
        Map sysIdsMap = json.parseObject(result, Map.class);
        String data = sysIdsMap.get("data").toString();
        Map sysIds = json.parseObject(data, Map.class);

        String items = sysIds.get("items").toString();
        List list = json.parseObject(items, List.class);
        //Map sysId= json.parseObject(data,Map.class);
        for (int i=0;i<list.size();i++){
            String quan = list.get(i).toString();
            Map values = json.parseObject(quan, Map.class);
            String pin = values.get("pin").toString();
            String checktimepin = values.get("checktime").toString();
            Attend attend = new Attend();
            attend.setTime(checktimepin);
            UsersMapper.
            //attend.set
            //String rmsRollRoom = JSONArray.parseObject((String) ename).getString("rmsRollRoom");
            //String rmsRollRoom = JSONArray.parseObject(ename).getString("rmsRollRoom");
            //Object pin =  list.get(Integer.parseInt("pin"));
        }*/
    }

    public static String sendPost(String url, String param) {
        String result = "";
        CloseableHttpClient httpClient =  HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("Accept", "application/json");
            httppost.setHeader("Content-Type","application/x-www-form-urlencoded");
            BasicHttpEntity requestBody = new BasicHttpEntity();
            requestBody.setContent(new ByteArrayInputStream(param.getBytes("UTF-8")));
            requestBody.setContentLength(param.getBytes("UTF-8").length);
            httppost.setEntity(requestBody);
// 执行客户端请求
            response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
            //自动释放链接
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭连接
            // httpclient.getConnectionManager().shutdown();
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  result;
    }
}